Regular Expression Square Bracket

Hello Guys,

i hope you can help me.

My Intention is only to print the parts in a string, which contains "[text]", so only those with a square bracket.

 

Regexp re = regexp2 "\\[.*?]"     //This one only prints "[text]"

Regexp re2 = regexp2 "\\[.*]"    //This one would print "[text] ,..-&%$ [text2],[text3]", so everything in between

string str = "blubb [text] ,..-&%$ [text2],[text3]..."   

 

So my goal is a regular expression which prints "[text] [text2] [text3]"

 

rest of the code would be

 

if (re str) {
print str[match 0]

}

 

Thanks in advance, i hope you guys can help me.


Auron555 - Wed Dec 17 09:56:55 EST 2014

Re: Regular Expression Square Bracket
Mathias Mamsch - Wed Dec 17 13:10:17 EST 2014

You need to iterate the regexp using the "search" perm ... You will find plenty of examples of looping over a buffer to find every match for a regexp on the forum.

Regards, Mathias

Re: Regular Expression Square Bracket
Auron555 - Thu Dec 18 04:26:44 EST 2014

Hallo Mathias, thanks for the answer.

 

I dont understand why this should resolve my problem, because my regexp obviously is wrong, is it not ?

 

Or must i use the first one ("re") and then must iterate so it finds everything?

 

I would want to know which regexp i must use, the rest i can manage with your suggestion :)

Re: Regular Expression Square Bracket
Tony_Goodman - Thu Dec 18 08:37:06 EST 2014

Auron555 - Thu Dec 18 04:26:44 EST 2014

Hallo Mathias, thanks for the answer.

 

I dont understand why this should resolve my problem, because my regexp obviously is wrong, is it not ?

 

Or must i use the first one ("re") and then must iterate so it finds everything?

 

I would want to know which regexp i must use, the rest i can manage with your suggestion :)

Try this

 

string s = "[text4] this string contains [text1] and [text2] etc [text 5]"

Regexp re = regexp2 "(\\[[^\\[]*\\]).*"

while (re s)
{
    print s[match 1]

 s = s[end 1 + 1:]
}

 

Re: Regular Expression Square Bracket
Auron555 - Fri Jan 09 01:55:13 EST 2015

Tony_Goodman - Thu Dec 18 08:37:06 EST 2014

Try this

 

string s = "[text4] this string contains [text1] and [text2] etc [text 5]"

Regexp re = regexp2 "(\\[[^\\[]*\\]).*"

while (re s)
{
    print s[match 1]

 s = s[end 1 + 1:]
}

 

Thank You very much Tony, its working ;)